import UIKit

class ProgressViewExampleViewController: UIViewController {
    
    // MARK: - 控件属性
    private let progressView = UIProgressView(progressViewStyle: .default)
    private let progressLabel = UILabel()
    private let startButton = UIButton(type: .system)
    private let resetButton = UIButton(type: .system)
    
    // 进度相关变量
    private var progress: Float = 0.0
    private var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UIProgressView
        progressView.translatesAutoresizingMaskIntoConstraints = false
        progressView.progress = progress  // 初始进度
        progressView.tintColor = .systemBlue  // 进度条颜色
        progressView.trackTintColor = .systemGray3  // 背景轨道颜色
        progressView.layer.cornerRadius = 4.0  // 圆角
        progressView.clipsToBounds = true
        view.addSubview(progressView)
        
        // 创建进度标签
        progressLabel.translatesAutoresizingMaskIntoConstraints = false
        progressLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        progressLabel.textAlignment = .center
        progressLabel.text = "进度: 0%"
        view.addSubview(progressLabel)
        
        // 创建开始按钮
        startButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.setTitle("开始", for: .normal)
        startButton.backgroundColor = .systemBlue
        startButton.tintColor = .white
        startButton.layer.cornerRadius = 8
        startButton.addTarget(self, action: #selector(startProgress), for: .touchUpInside)
        view.addSubview(startButton)
        
        // 创建重置按钮
        resetButton.translatesAutoresizingMaskIntoConstraints = false
        resetButton.setTitle("重置", for: .normal)
        resetButton.backgroundColor = .systemGray
        resetButton.tintColor = .white
        resetButton.layer.cornerRadius = 8
        resetButton.addTarget(self, action: #selector(resetProgress), for: .touchUpInside)
        view.addSubview(resetButton)
    }
    
    // MARK: - 布局约束
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // ProgressView 约束
            progressView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin * 2),
            progressView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin * 2),
            progressView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            progressView.heightAnchor.constraint(equalToConstant: 8),
            
            // 进度标签约束
            progressLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            progressLabel.bottomAnchor.constraint(equalTo: progressView.topAnchor, constant: -margin),
            
            // 开始按钮约束
            startButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin * 2),
            startButton.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: margin * 2),
            startButton.heightAnchor.constraint(equalToConstant: 44),
            
            // 重置按钮约束
            resetButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin * 2),
            resetButton.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: margin * 2),
            resetButton.heightAnchor.constraint(equalToConstant: 44),
            resetButton.leadingAnchor.constraint(equalTo: startButton.trailingAnchor, constant: margin)
        ])
    }
    
    // MARK: - 交互逻辑
    @objc private func startProgress() {
        // 禁用开始按钮，防止重复点击
        startButton.isEnabled = false
        startButton.backgroundColor = .systemGray
        
        // 创建计时器，每0.1秒更新一次进度
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
    }
    
    @objc private func updateProgress() {
        // 增加进度
        progress += 0.01
        
        // 更新进度条和标签
        progressView.setProgress(progress, animated: true)
        progressLabel.text = "进度: \(Int(progress * 100))%"
        
        // 检查是否完成
        if progress >= 1.0 {
            timer?.invalidate()
            timer = nil
            
            // 恢复按钮状态
            startButton.isEnabled = true
            startButton.backgroundColor = .systemBlue
            
            // 显示完成状态
            progressLabel.text = "进度: 完成!"
            progressView.tintColor = .systemGreen
            
            // 2秒后自动重置
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                self.resetProgress()
            }
        }
    }
    
    @objc private func resetProgress() {
        // 停止计时器
        timer?.invalidate()
        timer = nil
        
        // 重置进度
        progress = 0.0
        progressView.setProgress(progress, animated: false)
        progressLabel.text = "进度: 0%"
        
        // 恢复按钮状态
        startButton.isEnabled = true
        startButton.backgroundColor = .systemBlue
        
        // 恢复进度条颜色
        progressView.tintColor = .systemBlue
    }
}
